import Vue from 'vue'
import App from './App'
import router from './libs/router'
import md5 from 'md5'

import VConsole from 'vconsole'
//new VConsole();

//修改路由参数
import merge from 'webpack-merge';
Vue.prototype.merge=merge;

//复制内容
import VueClipboard from 'vue-clipboard2';
Vue.use(VueClipboard);

//图片地址
Vue.prototype.imgSrcUrl='';

//通用组件
import '@/libs/components';

Vue.config.productionTip = false;
Vue.prototype.md5=md5;

//mint-ui
import 'mint-ui/lib/style.css';
import { Picker,Toast,MessageBox,Indicator } from 'mint-ui';
Vue.component(Picker.name,Picker);
window['Toast']=Toast;
window['MessageBox']=MessageBox;
window['Indicator']=Indicator;

//swiper
import VueAwesomeSwiper from 'vue-awesome-swiper';
import 'swiper/dist/css/swiper.css';
Vue.use(VueAwesomeSwiper, );

//接口
import axios from 'axios';
Vue.prototype.axios=axios;
Vue.prototype.axios.defaults.withCredentials = true;

import api from '@/libs/api';
import http from '@/axios/http';

Vue.prototype.api=api;
Vue.prototype.http=http;
Vue.prototype.loading=true;

//懒加载
import VueLazyload from 'vue-lazyload';
Vue.use(VueLazyload);

//下拉加载更多
Vue.directive('viewmore', {
  bind: (el, binding) => {
    el.addEventListener('scroll', () => {
      if(el.scrollTop+el.offsetHeight+window.screen.height*0.3>=el.scrollHeight){
        binding.value();
      }
    }, false)
  }
});

let eventFn=null;
Vue.prototype.viewmore=(fn,type)=>{
  if(eventFn==null){
    eventFn=ev=>{
      let el=ev.target.body;
      if((el.scrollTop||document.documentElement.scrollTop)+el.offsetHeight+window.screen.height*0.3>=el.scrollHeight){
        fn();
      }
    };
  }
  if(type=='add'){
    window.addEventListener('scroll',eventFn);
  }else{
    window.removeEventListener('scroll',eventFn);
  }
};

//判断设备
let u = navigator.userAgent;
let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
function isUa(){
  if(isiOS){
    return 'ios';
  }else if(isAndroid){
    return 'android';
  }else{
    return 'web';
  }
};

Vue.prototype.isUa=isUa();

//微信sdk
import wxSdk from 'weixin-js-sdk';
Vue.prototype.wxSdk=wxSdk;

//VUEX
import store from '@/vuex';

//通用组件
import utils from '@/libs/utils';
Vue.use(utils);

//NProgress
import NProgress from 'nprogress';

//页面跳转后执行
router.beforeEach((to,from,next) => {
  NProgress.start();
  next();
});
router.afterEach((to,from,next) => {
  NProgress.done();
  document.title=to.meta.title;
});

//埋点
let _hmt = _hmt||[];
window._hmt=_hmt;
(()=>{
  let hm=document.createElement("script");
  hm.src="https://hm.baidu.com/hm.js?"+ '12bac22a04f1f40cd025945a801a05b6';
  let s=document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(hm,s);
})();

new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>',
  store
});

//rem屏幕兼容
const baseSize=100;
function setRem(){
  let scale;
  if(document.documentElement.clientWidth<1000){
    scale=document.documentElement.clientWidth/750;
  }else{
    scale=1;
  }
  document.documentElement.style.fontSize=(baseSize * Math.min(scale, 2))+'px';
};
setRem();
window.onresize=()=>{
  setRem();
};

//键盘收起
document.body.addEventListener('focusout',function(){
  setTimeout(function(){
    document.body.scrollTop=0;
  },200)
});

(function(){
  if (typeof WeixinJSBridge == "object" && typeof WeixinJSBridge.invoke == "function") {
    handleFontSize();
  } else {
    if (document.addEventListener) {
      document.addEventListener("WeixinJSBridgeReady", handleFontSize, false);
    } else if (document.attachEvent) {
      document.attachEvent("WeixinJSBridgeReady", handleFontSize);
      document.attachEvent("onWeixinJSBridgeReady", handleFontSize);  }
  }
  function handleFontSize() {
    // 设置网页字体为默认大小
    WeixinJSBridge.invoke('setFontSizeCallback', { 'fontSize' : 0 });
    // 重写设置网页字体大小的事件
    WeixinJSBridge.on('menu:setfont', function() {
      WeixinJSBridge.invoke('setFontSizeCallback', { 'fontSize' : 0 });
    });
  }
})();
